package com.dream.utils.random.validate;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dream.utils.cookie.CookieUtils;
import com.dream.utils.log.DreamLogUtils;

/**
 * random the validate code
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2012/8/18
 */
public class RandomValidateCodeUtils {
	
	public static final String RANDOM_IMAGE_VALIDATE = "random_image_validate";
    private static final Random random = new Random();
    private static final String CHARACTER_KEYS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int WIDTH = 80;//图片宽
    private static final int HEIGHT = 30;//图片高
    private static final int CHARACTER_NUM = 4;//随机产生字符数量
    /*
     * 获得字体
     */
    private static Font getFont(){
        return new Font("Fixedsys",Font.CENTER_BASELINE,18);
    }
    /*
     * 获得颜色
     */
    private static Color getRandColor(int fc,int bc){
        if(fc > 255)
            fc = 255;
        if(bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc-fc-16);
        int g = fc + random.nextInt(bc-fc-14);
        int b = fc + random.nextInt(bc-fc-18);
        return new Color(r,g,b);
    }
    /**
     * 生成随机图片
     */
    public static void outRandomImage(HttpServletRequest request,HttpServletResponse response) {
    	
    	String randomString = "";
        try {
        	
	        //BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
	        BufferedImage bfImage = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_BGR);
	        //产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
	        Graphics graphics = bfImage.getGraphics();
	        
	        graphics.fillRect(0, 0, WIDTH, HEIGHT);
	        graphics.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
	        //graphics.setColor(getRandColor(110, 150));
	        
	        //绘制干扰线
	        drawLine(graphics);
	        
	        //绘制随机字符
	        for(int i=0;i<CHARACTER_NUM;i++){
	            randomString = drawString(graphics,randomString,i);
	        }
	        
	        CookieUtils.addCookie(response, request, RANDOM_IMAGE_VALIDATE, randomString);
	        HttpSession session = request.getSession(true);
	        session.setAttribute(RANDOM_IMAGE_VALIDATE, randomString);
	        
	        graphics.dispose();
	        //将内存中的图片通过流动形式输出到客户端
            ImageIO.write(bfImage, "JPEG", response.getOutputStream());
           
        } catch (Exception e) {
        	DreamLogUtils.error(RandomValidateCodeUtils.class, "outRandomImage", "", e);
        }
    }
    /*
     * 绘制字符串
     */
    private static String drawString(Graphics g,String randomString,int i){
        g.setFont(getFont());
        g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
        String rand = String.valueOf(getRandomString(random.nextInt(CHARACTER_KEYS.length())));
        randomString +=rand;
        g.translate(random.nextInt(3), random.nextInt(3));
        g.drawString(rand, 18*i+5, 20);
        return randomString;
    }
    /*
     * 绘制干扰线
     */
    private static void drawLine(Graphics g){
       for(int i=0; i<WIDTH;i++){
    	   g.setColor(getRandColor(110, 150));
    	   g.drawLine(i, 0, i,HEIGHT);
       }
    }
    /*
     * 获取随机的字符
     */
    private static String getRandomString(int num){
        return String.valueOf(CHARACTER_KEYS.charAt(num));
    }
}
